function [code] = convenc(msg, trellis)
  
  msgSize1 = size(msg, 1);
  msgSize2 = size(msg, 2);
  if msgSize1 ~= 1 & msgSize2 ~= 1
    error('input msg must be a row or column vector');
  end
  
  regNum = log2(trellis.numStates)
  msg = [msg(:); zeros(regNum, 1)];
  if msgSize1 == 1
    codeSize1 = 1
    codeSize2 = msgSize2 + regNum
    msgLen = msgSize2
    codeLen = codeSize2
  else
    codeSize1 = msgSize1 + regNum
    codeSize2 = 1
    msgLen = msgSize1
    codeLen = codeSize1
  end
  
  code = zeros(codeLen, 1);
  curState = 0
  
  for i = 1:codeLen
    code(i) = trellis.outputs(curState+1, msg(i)+1)
    curState = trellis.nextStates(curState+1, msg(i)+1)
  end
  
  code = matrix(code, codeSize1, codeSize2);
  
endfunction

